<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->

      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="名称" prop="label">
            <el-input
              v-model="queryParams.label"
              placeholder=""
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>

      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              >修改</el-button
            >
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            >
          </el-col> -->
        </el-row>

        <el-table v-loading="loading" :data="tableData" stripe border size="small">
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column label="名称" key="label" prop="label" />

          <el-table-column
            label="值"
            key="value"
            prop="value"
            :show-overflow-tooltip="true"
          />

          <el-table-column label="状态" key="state" prop="state">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-value="0"
                inactive-value="1"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column label="排序" key="itemOrder" prop="itemOrder" />

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleStateChange(scope.row)"
                >启停用</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.size"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <dictitemdialog ref="dictitemdialog"></dictitemdialog>
  </div>
</template>

<script>
import { dictitemService } from "@/api/sysService";
import dictitemdialog from "./dictitemdialog.vue";

export default {
  name: "sconfg",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  // props: ["dictId"],
  components: { dictitemdialog },
  data() {
    return {
      dictId: "",

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      tableData: null,

      // 查询参数
      queryParams: {
        page: 1,
        size: 10,
        label: undefined,
        phone: undefined,
        status: undefined,
        deptId: undefined,
      },
    };
  },

  created() {
    this.dictId = this.$route.query.dictId || "";
    this.getList();
    // this.getDeptTree();
    // this.getConfigKey("sys.user.initPassword").then(response => {
    //   this.initPassword = response.msg;
    // });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      dictitemService
        .list({ dictId: this.dictId, ...this.queryParams })
        .then((response) => {
          this.tableData = response.data.data;
          this.total = response.data.total;
          this.loading = false;
        });
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },

    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        username: undefined,
        nickName: undefined,
        password: undefined,
        phone: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.dictitemdialog
        .open({
          dictId: this.dictId,
        })
        .then((_) => {
          this.getList();
        });
    },
    handleUpdate(row) {
      let data = JSON.parse(JSON.stringify(row));
      this.$refs.dictitemdialog.open(data).then((_) => {
        this.getList();
      });
    },

    handleStateChange({ id }) {
      this.$modal
        .confirm("是否确认改变状态？")
        .then(function () {
          return dictitemService.postStateChange({ id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
  },
};
</script>
